package com.nuanshui.heatedloan.msgcenter.service.apppush;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.nuanshui.framework.utils.redis.JedisUtil;
import com.nuanshui.heatedloan.constant.CacheConstants;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.constant.TransactionManagerType;
import com.nuanshui.heatedloan.msgcenter.dal.mapper.AppPushMsgMapper;
import com.nuanshui.heatedloan.msgcenter.facade.*;
import com.nuanshui.heatedloan.msgcenter.form.AppPushMsgForm;
import com.nuanshui.heatedloan.msgcenter.integration.MsgTools;
import com.nuanshui.heatedloan.msgcenter.model.*;
import com.nuanshui.heatedloan.msgcenter.vo.AppPushMsgVO;
import com.nuanshui.heatedloan.pageable.Pager;
import com.nuanshui.heatedloan.task.facade.QuartzTaskManager;
import com.nuanshui.heatedloan.task.facade.bean.JobParams;
import com.nuanshui.heatedloan.task.facade.bean.JobProvider;
import com.nuanshui.heatedloan.task.facade.constant.TaskEnum;
import com.nuanshui.heatedloan.userinfo.facade.UserQueryService;
import com.nuanshui.heatedloan.userinfo.facade.UserTokenService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 管理员推送信息 service
 * @param 
 * @author guohao
 * @date 2017年1月12日 上午10:40:11
 * @since $
 */
@Service("appPushMsgService")
@Transactional(readOnly=true,value=TransactionManagerType.MSGCENTER_TRANSACTION_MARGER)
public class AppPushMsgServiceImpl implements AppPushMsgService {

	private Logger logger= LoggerFactory.getLogger(AppPushMsgServiceImpl.class);

	@Autowired
	private AppPushMsgMapper appPushMsgMapper;

	@Reference(url=GlobalParam.USER_DUBBO_SERVER,timeout=GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.USER_DUBBO_SERVER_VERSION)
	private UserQueryService userQueryService;
	
	@Reference(url=GlobalParam.TASK_DUBBO_SERVER,timeout=GlobalParam.DUBBO_SERVER_TIME_OUT,version=GlobalParam.TASK_DUBBO_SERVER_VERSION)
	private QuartzTaskManager quartzTaskManager;

	@Reference(url=GlobalParam.USER_DUBBO_SERVER,timeout=GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.USER_DUBBO_SERVER_VERSION)
	private UserTokenService userTokenService;

	@Autowired
	private JedisUtil jedisUtil;

	@Override
	@Transactional(readOnly=false, value=TransactionManagerType.MSGCENTER_TRANSACTION_MARGER)
	public void saveAndPush(AppPushMsgForm appPushMsg) {
		if(appPushMsg.getPushTime() == null) {
			appPushMsg.setPushTime(new Date());
		}

		save(appPushMsg);

		//发送定时任务进行定时推送
		JobProvider jobProvider = new JobProvider(TaskEnum.ADMIN_PUSH_MSG.getName(),TaskEnum.ADMIN_PUSH_MSG.getGroup());
		jobProvider.addParams(JobParams.BIZ_KEY, appPushMsg.getId());
		jobProvider.addParams("env", appPushMsg.getEnv());

		quartzTaskManager.addJob(jobProvider, appPushMsg.getPushTime());
	}

	@Transactional(readOnly=false, value=TransactionManagerType.MSGCENTER_TRANSACTION_MARGER)
	public void save(AppPushMsgForm appPushMsg){
		appPushMsgMapper.save(appPushMsg);

		jedisUtil.setObject(CacheConstants.PUSH_MSG_MANAGE+appPushMsg.getId(),appPushMsg);
	}

	@Override
	public Pager<AppPushMsgVO> list(Pager<AppPushMsgVO> pager) {
		List<AppPushMsgVO> list = appPushMsgMapper.list(pager);
		pager.setData(list);
		return pager;
	}

	@Override
	public AppPushMsgVO load(String id) {
		return appPushMsgMapper.load(id);
	}

	@Override
	public AppPushMsgForm loadCacheMsg(String id) {
		return jedisUtil.getObject(CacheConstants.PUSH_MSG_MANAGE+id,AppPushMsgForm.class);
	}

}
